let app = getApp();
let util = app.globalData.util;
let wxApi = app.globalData.wxApi;
let Api = app.globalData.Api;
Page({
  data: {
    progress: 0,
    second: 0,
    secondVal: 0,
    randomCodes: [],
    curCode: null,
    show: false
  },
  onShow() {
    wxApi.setNavTitle('开始测试')
    this.init( this.level );
  },
  onLoad( options ) {
    this.level = JSON.parse( options.level );
    this.type = options.type;
  },
  onHide() {
    this.clearTimer();
  },
  onUnload() {
    this.clearTimer();
  },
  back() {
    this.clearTimer();
    this.backVal = true;
    wx.navigateBack({
      delta: 1
    })
  },
  init( level ) {
    let second = level.second;
    // 如果是点击返回，则不继续往下执行
    if ( this.backVal ) return;
    this.setData({
      second: second,
      progress: 0,
      secondVal: second,
      randomCodes: []
    })
    Api.traningCodesList(this.type, codes => {
      let randomCodes = this.getRandomCodes( codes );
      this.setData({
        randomCodes: randomCodes,
        show: true
      })
      let self = this;
      let timer = setTimeout(() => {
        self.countDown( second );
        clearTimeout( timer );
        timer = null;
      }, 500);
    }) 
  },
  getRandomCodes( codes ) {
    let arrKeys = [];
    let randomCodes = [];
    for (let i = 0; i < 20; i++) {
      let index = util.randomNum( 0, codes.length - 1 );
      if ( arrKeys.indexOf( index ) == -1 ) {
        arrKeys.push( index );
        randomCodes.push( codes[ index ] );
      } else {
        i --;
      }
    }
    console.log( randomCodes )
    return randomCodes;
  },
  countDown( second ) {
    let tolSecond = this.data.second;
    console.log( tolSecond );
    this.timer1 = setInterval(() => {
      this.setData({
        progress: Math.floor(((tolSecond - second) / tolSecond) * 100),
        secondVal: second
      })
      console.log( Math.floor(((tolSecond - second) / tolSecond) * 100) )
      second --;
      if ( second < 0 ) {
        clearInterval( this.timer1 );
        this.timer1 = null;
        this.gotoTestAccount( this.data.randomCodes );
      }
    }, 1000);
    this.cartCountDown( second );
  },
  cartCountDown( second ) {
    let count = 20;
    let tolSecond = this.data.second;
    let codes = this.data.randomCodes;
    let curCount = 0;
    this.setData({
      curCode: codes[ curCount ]
    })
    this.timer2 = setInterval(() => {
      curCount ++;
      this.setData({
        curCode: codes[ curCount ]
      })
      console.log( curCount );
      if ( curCount == count - 1 ) {
        clearInterval( this.timer2 );
        this.timer2 = null;
        
        return; 
      }
    }, (second / 20) * 1000)
  },
  gotoTestAccount( codes ) {
    
    let codesURI = util.encodeURI( codes );
    wxApi.navigateTo( '../test-account/test-account', {
      codes: codesURI
    } )
    this.setData({
      randomCodes: [],
      curCode: null
    })
  },
  loadCodeList() {
    
  },
  clearTimer() {
    clearInterval( this.timer1 );
    clearInterval( this.timer2 );
    this.timer1 = null;
    this.timer2 = null;
  }
})